package cn.lysj.framework.common.utils.string;

import java.util.Collections;
import java.util.Set;
import java.util.TreeSet;

/**
 * 该类提供多个主键ID与字符串的相互转化
 * 
 * @author LinLion
 * 
 */
public class LongSetAndStringConvertUtil {

	/**
	 * 将Set<Long>集合转化为String,并用'-'隔开
	 * 
	 * @param ids:Set<Long>
	 * @return string字符串:'-123-124-225-'
	 */
	public static String longSet2String(Set<Long> ids) {
		if (ids == null || ids.size() == 0) {
			return "";
		}
		StringBuilder sb = new StringBuilder("-");
		for (Long id : ids) {
			sb.append(String.valueOf(id));
			sb.append("-");
		}
		return sb.toString();
	}

	/**
	 * 将String转化为Set<Long>,要求字符串用'-'隔开
	 * 
	 * @param string:字符串:'-123-124-225-'
	 * @return Set<Long>
	 */
	public static Set<Long> string2LongSet(String string) {
		if (string == null || string.trim().equals("")) {
			return Collections.emptySet();
		}
		String[] ids = string.split("-");
		Set<Long> set = new TreeSet<Long>();
		for (int i = 0; i < ids.length; i++) {
			if (ids[i] == null || ids[i].equals("")) {
				continue;
			}
			set.add(Long.valueOf(ids[i]));
		}
		return set;
	}
}
